package vip.zhonghui.www.skills

import android.annotation.SuppressLint
import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.google.gson.Gson
import org.json.JSONObject

class ActivityList : AppCompatActivity() {
    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)
        val sp = getSharedPreferences("local", MODE_PRIVATE)
        val ed = sp.edit()
        val from = findViewById<TextView>(R.id.from)
        val to = findViewById<TextView>(R.id.to)
        val date = findViewById<TextView>(R.id.date)

        val btn_time = findViewById<TextView>(R.id.btn_time)
        val btn_pirice = findViewById<TextView>(R.id.btn_pirice)
        val img_time = findViewById<ImageView>(R.id.img_time)
        val img_price = findViewById<ImageView>(R.id.img_price)
        val list_box = findViewById<ListView>(R.id.list_box)


        findViewById<ImageView>(R.id.retur).setOnClickListener { finish() }

        from.text=sp.getString("from","")
        to.text=sp.getString("to","")
        date.text="${sp.getString("year","")}年${sp.getString("month","")}月${sp.getString("dayOfMonth","")}日"


        Http.post("ticket", JSONObject().apply {
            put("token",sp.getString("token",""))
            put("from",from.text.toString())
            put("to",to.text.toString())
            put("time","${sp.getString("year","")}/${sp.getString("month","")}/${sp.getString("dayOfMonth","")}")
        }){
                resp -> val data = Gson().fromJson(resp,Data::class.java)

                runOnUiThread {
                    val listData = ArrayList<ListAdapter.Item>()

                    for (i in data.data.userInfo.data){
                        listData.add(ListAdapter.Item(i.starttime,i.endtime,i.lowestprice,i.name,i.number))
                    }

                    for ( i in 0 until listData.size)
                    for ( i in 0 until listData.size-1){
                        if (listData[i].T1.substring(0,2).toInt() > listData[i+1].T1.substring(0,2).toInt()){
                            val t = listData[i]
                            listData[i] = listData[i+1]
                            listData[i+1] = t
                        }else if (listData[i].T1.substring(0,2).toInt() == listData[i+1].T1.substring(0,2).toInt()){
                            if (listData[i].T1.substring(3,5).toInt() == listData[i+1].T1.substring(3,5).toInt()){
                                val t = listData[i]
                                listData[i] = listData[i+1]
                                listData[i+1] = t
                            }
                        }
                    }
                   val listAdapter = ListAdapter(this,R.layout.item,listData)
                    list_box.adapter=listAdapter



                    btn_time.setOnClickListener {

                       if (listData[0].T1.substring(0,2).toInt() > listData[listData.size-1].T1.substring(0,2).toInt()) {

                           img_time.setImageResource(R.mipmap.dwon)
                           for ( i in 0 until listData.size)
                               for ( i in 0 until listData.size-1){
                                   if (listData[i].T1.substring(0,2).toInt() > listData[i+1].T1.substring(0,2).toInt()){
                                       val t = listData[i]
                                       listData[i] = listData[i+1]
                                       listData[i+1] = t
                                   }else if (listData[i].T1.substring(0,2).toInt() == listData[i+1].T1.substring(0,2).toInt()){
                                       if (listData[i].T1.substring(3,5).toInt() > listData[i+1].T1.substring(3,5).toInt()){
                                           val t = listData[i]
                                           listData[i] = listData[i+1]
                                           listData[i+1] = t
                                       }
                                   }
                               }
                           val listAdapter = ListAdapter(this,R.layout.item,listData)
                           list_box.adapter=listAdapter
                       }else{

                           img_time.setImageResource(R.mipmap.up)
                           for ( i in 0 until listData.size)
                               for ( i in 0 until listData.size-1){
                                   if (listData[i].T1.substring(0,2).toInt() < listData[i+1].T1.substring(0,2).toInt()){
                                       val t = listData[i]
                                       listData[i] = listData[i+1]
                                       listData[i+1] = t
                                   }else if (listData[i].T1.substring(0,2).toInt() == listData[i+1].T1.substring(0,2).toInt()){
                                       if (listData[i].T1.substring(3,5).toInt() < listData[i+1].T1.substring(3,5).toInt()){
                                           val t = listData[i]
                                           listData[i] = listData[i+1]
                                           listData[i+1] = t
                                       }
                                   }
                               }
                           val listAdapter = ListAdapter(this,R.layout.item,listData)
                           list_box.adapter=listAdapter

                       }




                    }

                    btn_pirice.setOnClickListener {

                        if (listData[0].price.toInt() > listData[listData.size-1].price.toInt()){
                            img_price.setImageResource(R.mipmap.up)
                            for ( i in 0 until listData.size)
                                for ( i in 0 until listData.size-1){
                                    if (listData[i].price.toInt() > listData[i+1].price.toInt()){
                                        val t = listData[i]
                                        listData[i] = listData[i+1]
                                        listData[i+1] = t
                                    }
                                }
                            val listAdapter = ListAdapter(this,R.layout.item,listData)
                            list_box.adapter=listAdapter
                        }else{

                            img_price.setImageResource(R.mipmap.dwon)
                            for ( i in 0 until listData.size)
                                for ( i in 0 until listData.size-1){
                                    if (listData[i].price.toInt() < listData[i+1].price.toInt()){
                                        val t = listData[i]
                                        listData[i] = listData[i+1]
                                        listData[i+1] = t
                                    }
                                }
                            val listAdapter = ListAdapter(this,R.layout.item,listData)
                            list_box.adapter=listAdapter
                        }

                    }















                }

        }




    }
    class ListAdapter(val activity: Activity,val resouceID:Int,val data:ArrayList<Item>):ArrayAdapter<ListAdapter.Item>(activity,resouceID,data){
        @SuppressLint("ViewHolder")
        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            fun img(text:String): Int {
                return when(text){
                    "南方航空"->R.mipmap.nanfang
                    "山东航空"->R.mipmap.shandong
                    "深圳航空"->R.mipmap.shenzhegn
                    "四川航空"->R.mipmap.sican
                    "厦门航空"->R.mipmap.xiamen
                    "春秋航空"->R.mipmap.cunqiu
                    else -> R.drawable.blank
                }
            }
            val v = LayoutInflater.from(activity).inflate(resouceID,parent,false)
            val item = data[position]
            v.findViewById<TextView>(R.id.t1).text = item.T1
            v.findViewById<TextView>(R.id.t2).text = item.T2
            v.findViewById<TextView>(R.id.price).text = "￥"+item.price
            v.findViewById<TextView>(R.id.name).text = "${item.name}      ${item.name}"
            v.findViewById<ImageView>(R.id.img).setImageResource(img(item.name))
            return v
        }




        class Item(val T1:String,val T2:String,val price:String,val name: String,val num:String)
    }


    data class Data(
        val `data`:data3
    )
    data class data3(
        val userInfo:userInfo
    )
    data class userInfo(
        val `data`:ArrayList<data2>
    )
    data class data2(
        val name:String,
        val starttime:String,
        val endtime:String,
        val number:String,
        val lowestprice:String,
    )
}